package com.igeek.d0619.test2;

import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: zhiyu
 * @Date:18:33
 * @DESC:
 */
public class ApplicationContext {
    private static Map<String,Object> map = new LinkedHashMap<>();
    public static void loadClass(Class<?> type) throws IllegalAccessException {
        map.put("p1",new Person("张三",23));
        Teacher teacher = new Teacher();
        Field[] fields = type.getDeclaredFields();
        for (Field f:fields) {
            f.setAccessible(true);
            if (f.isAnnotationPresent(MyValue.class)){
                if (String.valueOf(f.getType()).equals("int")){
                    f.set(teacher,Integer.parseInt(f.getDeclaredAnnotation(MyValue.class).value()));
                }else {

                    f.set(teacher,f.getDeclaredAnnotation(MyValue.class).value());
                }
            }else if (f.isAnnotationPresent(MyAutowired.class)){
                String s = f.getDeclaredAnnotation(MyAutowired.class).beanName();
                Object o = map.get(s);
                f.set(teacher,o);
            }
        }
        System.out.println(teacher.toString());
    }

    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        loadClass(Teacher.class);
    }
}
